package com.hejing.project02_拼电商客户管理系统;

/**
 * ClassName: CustomerView
 * Package: com.hejing.project02_拼电商客户管理系统
 * Description:
 *      CustomerView为主模块， 负责菜单的显示和处理用户操作。
 * @Author: HeJing
 * @Create: 2023-03-25 14:54
 * @Version: 1.0
 */
public class CustomerView {
    CustomerList customerList = new CustomerList(10);

    /**
     * 进入主界面的方法
     */
    public void enterMainMenu() {

        boolean isFlag = true;
        while(isFlag) {
            // 显示界面
            System.out.println("\n---------------- 拼电商客户管理系统 -----------------\n");
            System.out.println("                 1 添 加 客 户");
            System.out.println("                 2 修 改 客 户");
            System.out.println("                 3 删 除 客 户");
            System.out.println("                 4 客 户 列 表");
            System.out.println("                 5 退      出\n");
            System.out.print("                 请 选 择(1-5): ");

            char key = CMUtility.readMenuSelection();
            // 获取用户选择:
            switch(key) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomers();
                    break;
                case '5':
                    System.out.println("请确认是否退出系统(Y/N): ");
                    char isExit = CMUtility.readConfirmSelection();
                    if(isExit == 'Y') {
                        isFlag = false;
                        System.out.println("\n*************** 欢饮下次光临 **************");
                    }
                    break;
            }
        }
    }

    /**
     * 添加新的客户:
     */
    private void addNewCustomer() {
        System.out.println("------------------- 添 加 客 户 ------------------");
        System.out.print("姓名: ");
        String name = CMUtility.readString(4);
        System.out.print("性别: ");
        char gender = CMUtility.readChar();
        System.out.print("年龄: ");
        int age = CMUtility.readInt();
        System.out.print("电话: ");
        String phone = CMUtility.readString(11);
        System.out.print("邮箱: ");
        String email = CMUtility.readString(15);

        Customer cust = new Customer(name, gender, age, phone, email);
        boolean flag = customerList.addCustomer(cust);
        if(flag) {
            System.out.println("\n----------------- 客户对象添加完毕 ------------------");
        } else{
            System.out.println("\n----------------- 对象数组已满，无法添加 -------------");
        }
    }

    /**
     * 修改客户:
     */
    private void modifyCustomer() {
        System.out.println("------------------ 修改客户对象 ------------------\n");
        int index = 0;
        Customer cust = null;
        while(true){
            System.out.print("请输入想要修改的客户编号或退出该功能(-1退出): ");
            index = CMUtility.readInt();
            if(index == -1){
                return;
            }

            cust = customerList.getCustomer(index - 1);
            if(cust == null) {
                System.out.print("客户不存在--");
            } else break;
        }
        // 找到了待修改对象, 开始修改操作
        System.out.print("姓名(" + cust.getName() + "): ");
        String name = CMUtility.readString(4, cust.getName());  // 如果用户不输入字符而直接回车，则将cust.getname()作为返回值

        System.out.print("性别(" + cust.getGender() + "): ");
        char gender = CMUtility.readChar(cust.getGender());

        System.out.print("(年龄(" + cust.getAge() + "): ");
        int age = CMUtility.readInt(cust.getAge());

        System.out.print("电话(" + cust.getPhone() + "): ");
        String phone = CMUtility.readString(11, cust.getPhone());

        System.out.print("邮箱(" + cust.getEmail() + "): ");
        String email = CMUtility.readString(15, cust.getEmail());

        Customer temp = new Customer(name, gender, age, phone, email);
        boolean isReplace = customerList.replaceCustomer(index - 1, temp);
        if(isReplace) {
            System.out.println("*********** 成功修改客户 + " + index + "***********");
        } else{
            System.out.println("************* 修改客户失败 **************");
        }

    }

    /**
     * 删除客户
     */
    private void deleteCustomer() {
        System.out.println("--------------------- 删除客户 --------------------");
        int index = 0;
        while(true) {
            System.out.print("请输入想要删除的客户的编号或退出该功能(-1退出): ");
            index = CMUtility.readInt();
            if(index == -1) {
                break;
            }
            // 在对象数组中查找该对象是否存在:
            Customer cust = customerList.getCustomer(index - 1);
            if(cust != null) break;  // 成功找到了待删除元素
            else System.out.print("客户不存在--");
        }

        // 进行删除操作；
        boolean isDelete = customerList.deleteCustomer(index - 1);
        if(isDelete) {
            System.out.println("***************** 编号为index的对象删除成功 ***************");
        } else {
            System.out.println("该对象不存在");
        }
    }

    /**
     * 显示数组中客户信息
     */
    private void listAllCustomers() {
        System.out.println("\n------------------- 客户列表 -------------------");
        Customer[] custs = customerList.getAllCustomers();
        if(custs.length == 0) {
            System.out.println("************* 没有客户记录 *************");
        } else {
            System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
            for(int i = 0; i < custs.length; i++) {
                System.out.println((i + 1000) + "\t" + custs[i].getDetails());
            }
            System.out.println("\n************* 显示客户列表完毕 *************");
        }
    }

    public static void main(String[] args) {
        CustomerView view = new CustomerView();

        // 进入主界面
        view.enterMainMenu();

    }
}





























